﻿using System;

using log4net;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace ScenarioFramework.Portal.Modules.Menu
{
    public class MenuHelper
    {
        //SFMenu rootMenu = null;
        BO.Menus rootMenu = null;
        String sUser = string.Empty;

        String sDocumentName = string.Empty;
        String sDocument = string.Empty;

        private static readonly ILog Logger = LogManager.GetLogger(typeof(MenuHelper));
        FileSystemWatcher watcher = new FileSystemWatcher();

        /*void watcher_FileCreated(object sender, FileSystemEventArgs e)
        {
            if (e.Name.CompareTo(sDocumentName) != 0)
                return;

            watcher.EnableRaisingEvents = false;
            rootMenu = new SFMenu();
            try
            {
                rootMenu.Load(sDocument);
            }
            catch (XmlException err)
            {
                Logger.Error(String.Format("Impossible to retrieve menu file{0}", err.Message));
            }

            watcher.EnableRaisingEvents = true;
        }
        */
        public MenuHelper(string sUser)
        {
            Logger.Debug(String.Format("User[{0}]", sUser));

            String sMenuPath = String.Format ("{0}\\SF\\Configuration",HttpContext.Current.Server.MapPath("~"));

            sDocumentName = String.Format("{0}_Menu.xml", sUser);
            sDocument = String.Format("{0}\\{1}", sMenuPath, sDocumentName);
            if ( !File.Exists(sDocument)) {
                sDocumentName = "Menu.xml";
                sDocument = String.Format("{0}\\{1}", sMenuPath, sDocumentName);
            }

            //rootMenu = new SFMenu();
            try
            {
                
                //watcher.Path = sMenuPath;
                //watcher.Filter = "*.*";
                //watcher.Changed += new FileSystemEventHandler(watcher_FileCreated);
                //watcher.EnableRaisingEvents = true;

                TextReader reader = new StreamReader(sDocument);
                XmlSerializer serializer = new XmlSerializer(typeof(ScenarioFramework.Portal.Modules.Menu.BO.Menus));
                rootMenu = (ScenarioFramework.Portal.Modules.Menu.BO.Menus)serializer.Deserialize(reader);
                reader.Close();
                //rootMenu.Load(sDocument);
            }
            catch (XmlException err)
            {
                Logger.Error(String.Format ("Impossible to retrieve menu file{0}", err.Message));
            }
        }

        public BO.Menus RootMenu
        {
            get { return rootMenu; }
        }

        public BO.Menu Get(BO.Menu[] menus, string sMenuName)
        {
            BO.Menu menu;

            for (int i = menus.GetLowerBound(0); i <= menus.GetUpperBound(0); i++)
            {
                menu = (BO.Menu)menus[i];

                if (menu.Name == sMenuName)
                {
                    return menu;
                }
            }

            return null;
        }
    }
}
